package com.kunya.product.service.impl;

import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.DateTimeUtil;
import com.kunya.common.util.FTPUtil;
import com.kunya.common.util.MD5Util;
import com.kunya.product.dao.SliderMapper;
import com.kunya.product.po.Slider;
import com.kunya.product.service.ISliderService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Service("iSliderService")
public class SliderServiceImpl implements ISliderService {

    @Autowired
    private SliderMapper sliderMapper;

    /**
     * 显示logo图片
     *
     * @param slider
     * @return
     */
    @Override
    public ServerResponse select(Slider slider) {
        if (slider.getUserid() == null) {
            return ServerResponse.createByErrorMessage("必须传入userid");
        }
        /**
         * 当记录不存在时，返回错误码
         */
        Slider rslider = sliderMapper.selectByUserId(slider.getUserid());
        if (rslider == null) {
            return ServerResponse.createByError();
        } else {
            return ServerResponse.createBySuccess(rslider.getUrl());
        }
    }

    /**
     * 设置logo图片显示隐藏
     *
     * @param slider
     * @return
     */
    @Override
    public ServerResponse updateStatus(Slider slider) {
        if (slider.getUserid() == null) {
            return ServerResponse.createByErrorMessage("必须传入userid");
        }
        if (slider.getStatus() == null) {
            return ServerResponse.createByErrorMessage("必须传入status");
        }

        try {
            if (sliderMapper.updateByUserId(slider) == 0) {
                return ServerResponse.createByErrorMessage("更新失败");
            } else {
                return ServerResponse.createBySuccessMessage("更新成功");
            }
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("更新失败");
        }
    }

    /**
     * 删除图片
     *
     * @param slider
     * @return
     */
    @Override
    public ServerResponse delete(Slider slider) {
        if (slider.getUserid() == null) {
            return ServerResponse.createByErrorMessage("必须传入userid");
        }
        try {
            if (sliderMapper.delete(slider) == 0) {
                return ServerResponse.createByErrorMessage("删除失败");
            } else {
                return ServerResponse.createBySuccessMessage("删除成功");
            }
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("删除失败");
        }

    }

    @Override
    public ServerResponse uploadView(MultipartFile file, Integer id) throws IOException {
        //TODO 从Shrio当中获取当前登录的用户名
        String username="Suimg";
        String newFileName=UUID.randomUUID().toString()+"."+FilenameUtils.getExtension(file.getOriginalFilename()).toLowerCase();
        String urlPath = String.format("/%s/%s/",MD5Util.MD5EncodeUtf8(username),MD5Util.MD5EncodeUtf8(DateTimeUtil.getCurrentDate()));
        FTPUtil.upload(file.getInputStream(),urlPath,newFileName);
        Slider slider=new Slider();
        slider.setId(id);
        slider.setUrl(urlPath+newFileName);
        return sliderMapper.updateByPrimaryKeySelective(slider)==1?
                ServerResponse.createBySuccess("上传图片成功"):ServerResponse.createByErrorMessage("上传图片失败");
    }

}
